﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace FeedReader.Shared.Data
{
    public class LogDataSource
    {
        private LogDataServiceContext _ServiceContext = null;

        public LogDataSource(CloudStorageAccount storageAccount)
        {
            // var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new LogDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(LogDataServiceContext.LogTableName);
        }

        public IEnumerable<LogDataModel> Select()
        {
            var results = (from c in _ServiceContext.LogTable
                           select c).Take(100);

            var query = results.AsTableServiceQuery<LogDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        //public IEnumerable<LogDataModel> SelectPublic()
        //{
        //    var results = from c in _ServiceContext.LogTable
        //                  where c.Type == "Public"
        //                  orderby c.DateTime
        //                  select c;

        //    var query = results.AsTableServiceQuery<LogDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}
        //public IEnumerable<LogDataModel> WhereDate(DateTime date)
        //{
        //    var results = from c in _ServiceContext.LogTable
        //                  where c.DateTime < date
        //                  select c;

        //    var query = results.AsTableServiceQuery<LogDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}
        //public void Delete(LogDataModel itemToDelete)
        //{
        //    _ServiceContext.AttachTo(LogDataServiceContext.LogTableName, itemToDelete, "*");
        //    _ServiceContext.DeleteObject(itemToDelete);
        //    _ServiceContext.SaveChanges();
        //}
        //public void Insert(string action, string descriptions, string type, DateTime time)
        //{
        //    LogDataModel newitem = new LogDataModel();
        //    newitem.Action = action;
        //    newitem.DateTime = time;
        //    newitem.Description = descriptions;
        //    newitem.Type = type;
        //    _ServiceContext.AddObject(LogDataServiceContext.LogTableName, newitem);
        //    _ServiceContext.SaveChanges();
        //}
        //public void Insert(LogDataModel newItem)
        //{
        //    _ServiceContext.AddObject(LogDataServiceContext.LogTableName, newItem);
        //    _ServiceContext.SaveChanges();
        //}

        public void Insert(string p, string p_2, string p_3, DateTime dateTime)
        {
            throw new NotImplementedException();
        }
    }
}
